<template>
  <div>
    <el-button type="primary"  @click="() => append2()">
      添加
    </el-button>
    <p></p>
    <el-tree
      :data="menus"
      :props="defaultProps"
      :expand-on-click-node="false"
      show-checkbox
      node-key="id"
      :default-expanded-keys="expandedkey"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
     
          <span>{{ node.label }}</span>
          <span>{{ node.data.route }}</span>
      
        
        <span>
          <el-button type="success" size="mini" v-if="node.level <= 1" @click="() => append(data, node)" icon="el-icon-plus" plain>
            添加
          </el-button>
          
          <el-button size="mini" @click="() => edit(data)" type="primary"  icon="el-icon-edit"  plain >
            修改
          </el-button>
          <el-button type="danger" size="mini" @click="() => remove(node, data)"  icon="el-icon-delete" plain >
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
      <el-form :model="addmenu">
        <el-form-item label="分类名称">
          <el-input v-model="addmenu.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="路由路径">
          <el-input v-model="addmenu.route" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="() => submitData()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  //import引入的组件需要注入到对象中才能使用
  data() {
    return {
      dialogtype: "", //用于判断dialog的类型，对dialog进行复用
      title: "",
      dialogVisible: false,
      expandedkey: [],
      id: [],
      menus: [],
      addmenu: {
        id: null,
        parentId: 0,
        name: "",
        level: 1,
        route: "",
        showStatus: 0,
        sort: 0
      },
      defaultProps: {
        children: "children",
        label: "name"
      }
    };
  },
  methods: {
    //添加子菜单
    append(data, node) {
      // console.log("添加成功1", data, "node:",node);
      this.dialogtype = "add";
      this.title = "添加子菜单";
      this.dialogVisible = true;
      this.addmenu.parentId = data.id;
      this.addmenu.level = data.level * 1 + 1;

      this.addmenu.id = null;
      this.addmenu.name = "";
      this.addmenu.route = "";
      this.addmenu.showStatus = 0;
      this.addmenu.sort = 0;
    },
    //添加根菜单
    append2() {
      this.dialogtype = "add";
      this.title = "添加根菜单";
      this.dialogVisible = true;
      this.addmenu.parentId = 0;
      this.addmenu.level = 0;

      this.addmenu.id = null;
      this.addmenu.name = "";
      this.addmenu.route = "";
      this.addmenu.showStatus = 0;
      this.addmenu.sort = 0;
    },
    //获取菜单
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/menu/menu/list/tree"),
        method: "get"
      }).then(({ data }) => {
        // console.log("成功获取到分类数据", data.data);
        this.menus = data.data;
      });
    },
    //删除菜单
    remove(node, data) {
      var id = [data.id];
      //删除提醒
      this.$confirm(`是否删除【${data.name}】菜单`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/menu/menu/delete"),
            method: "post",
            data: this.$http.adornData(id, false)
          }).then(({ data }) => {
            // console.log("删除成功");
            this.getMenus(); //获取菜单->刷新
            //删除成功提醒
            this.$message({
              message: "栏目删除成功！",
              type: "success"
            });
            this.expandedkey = [node.parent.data.id];
          });
        })
        .catch(() => {});

      // console.log("append", node, data);
    },
    //对dialog的复用类型判断
    submitData() {
      console.log();
      if (this.dialogtype == "add") {
        this.addappendlist();
      }
      if (this.dialogtype == "edit") {
        this.addeditlist();
      }
    },
    //编辑菜单
    edit(data) {
      this.dialogtype = "edit";
      this.title = "修改菜单";
      this.dialogVisible = true;
      this.$http({
        url: this.$http.adornUrl(`/menu/menu/info/${data.id}`),
        method: "get"
      }).then(({ data }) => {
        console.log("成功获取到分类数据", data.menu.id);
        this.addmenu.id = data.menu.id;
        this.addmenu.name = data.menu.name;
        this.addmenu.route = data.menu.route;
        this.addmenu.parentId = data.menu.parentId;
      });
    },
    //提交编辑
    addeditlist() {
      // console.log("addedit,pro");
      var { id, name, route } = this.addmenu;
      this.$http({
        url: this.$http.adornUrl("/menu/menu/update"),
        method: "post",
        data: this.$http.adornData({ id, name, route }, false)
      }).then(({ data }) => {
        this.$message({
          message: "菜单修改成功",
          type: "success"
        });
        //关闭窗口
        this.dialogVisible = false;
        //刷新删除成功后的菜单
        this.getMenus();
        //设置需要默认展开的菜单
        this.expandedkey = [this.addmenu.parentId];
      });
    },
    //提交添加
    addappendlist() {
      // console.log(this.addmenu);
      this.$http({
        url: this.$http.adornUrl("/menu/menu/save"),
        method: "post",
        data: this.$http.adornData(this.addmenu, false)
      }).then(({ data }) => {
        this.$message({
          message: "菜单保存成功",
          type: "success"
        });

        //关闭窗口
        this.dialogVisible = false;
        //刷新删除成功后的菜单
        this.getMenus();
        //设置需要默认展开的菜单
        this.expandedkey = [this.addmenu.parentId];
      });
    }
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},

  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getMenus();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;

}
.el-tree-node__content{
   height: 35px;
}


</style>
